<?php

namespace app\api\controller;

use app\common\model\Attachment as AttachmentModel;
use app\model\Column as ColumnModel;
use think\Db;
use think\facade\Log;
use think\facade\Request;

/**
 * @title   栏目接口
 * @desc    栏目接口
 * Class Standard
 * @package app\api\controller
 */

class Column extends Common
{
    /**
     * @title 栏目列表
     * @desc  栏目列表
     *
     * @param int $title
     * @param int $limit 10
     *
     * @return Array $list 栏目列表
     */
    public function lists()
    {
        // try {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $limit = $this->request->param('limit');
        $year = $this->request->param('year');
        if (!$limit) {
            $limit = 12;
        }
        if (!$year) {
            $year = date('Y');
        }
        if ($year == 9999) {
            $year = '杂志';
        }
        $yearcol = ColumnModel::where('title', $year)->find();
        // ColumnModel::where('title', $year)->select();
        // return ColumnModel::getLastSql();

        //return $yearcol;
        if ($yearcol['title']) {
            $path = $yearcol['path'] . $yearcol['id'] . ',';
        } else {
            $yearcol2 = ColumnModel::where('title', $year - 1)->find();
            $path = $yearcol2['path'] . $yearcol2['id'] . ',';
        }
        //return $path;
        //$path = '0,2,76,';
        $list = ColumnModel::where('path', $path)
            ->field(['id', 'title', 'name', 'cover_picture'])
            ->order('id desc')
            ->paginate($limit);

        foreach ($list as $gq) {
            $Attachment = AttachmentModel::get($gq['cover_picture']);
            $gq['cover_picture'] = $this->getPicPath($Attachment['path']);
        }
        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }

    /**
     * 栏目信息
     * @return string
     */
    public function info()
    {
        // if ($this->request->isPost()) {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $id = $this->request->param('id');
        $limit = $this->request->param('limit');
        if ($id != '') {
            $map[] = ['id', '=', $id];
        }
        if (!$limit) {
            $limit = 10;
        }
        $list = ColumnModel::where($map)
            ->field(['id', 'title', 'name', 'cover_picture'])
            ->append(['content'])
            ->order('id desc')
            ->paginate($limit);

        foreach ($list as $gq) {
            $Attachment = AttachmentModel::get($gq['cover_picture']);
            $gq['cover_picture'] = $this->getPicPath($Attachment['path']);
            $gq['content'] = $this->getContent($gq['name']);
        }

        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }
}
